package vn.com.vtv.weather;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import org.openweathermap.model.DayInfo;
import org.openweathermap.model.DaysInfo;
import org.openweathermap.model.Temp;
import org.openweathermap.model.Weather;
import org.openweathermap.service.Constant;
import org.openweathermap.util.Language;

import vn.com.vtv.R;
import vn.hoasua.util.TimeUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;

/**
 * If you are familiar with Adapter of ListView,this is the same as adapter with
 * few changes
 * 
 */
public class ListProvider implements RemoteViewsFactory {
	private ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
	private DaysInfo daysInfo = null;
	private Context context = null;
	// private int appWidgetId;
	private Class<?> currentClass = null;
	public long lastUpdateTime = System.currentTimeMillis();

	public ListProvider(Context context) {
		this.context = context;
	}

	public void setDataContext(Intent intent) {
		// appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
		// AppWidgetManager.INVALID_APPWIDGET_ID);
		listItemList = new ArrayList<ListItem>();
		populateListItem();
	}

	public void setDataContext(ArrayList<ListItem> data) {
		listItemList.clear();
		for (ListItem item : data) {
			listItemList.add(item);
		}
		this.currentClass = ListItem.class;
		onDataSetChanged();
	}

	public void setDataContext(DaysInfo daysInfo) {
		this.daysInfo = daysInfo;
		this.currentClass = DaysInfo.class;
		onDataSetChanged();
	}

	private void populateListItem() {
		String[] day = { "", "", "", "", "", "", "" };
		String[] weather = { "", "", "", "", "", "", "" };
		String[] wind = { "", "", "", "", "", "", "" };
		Integer[] image = { R.drawable.rainy, R.drawable.cloudy, R.drawable.rainy, R.drawable.cloudy, R.drawable.rainy, R.drawable.cloudy, R.drawable.rainy };
		for (int i = 0; i < 7; i++) {
			ListItem listItem = new ListItem();
			listItem.heading = day[i];
			listItem.content = (i % 45 + new Random().nextInt(40)) + "" + (char) 0x00B0 + "C " + weather[i] + ". " + wind[i] + " km/h wind.";
			listItem.imageId = image[i];
			listItemList.add(listItem);
		}
	}

	@Override
	public int getCount() {
		/*
		 * if(currentClass == null || currentClass == ListItem.class){ return
		 * listItemList.size(); } if(currentClass == DaysInfo.class){ return
		 * daysInfo.list.length; }
		 */
		return 10;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/*
	 * Similar to getView of Adapter where instead of Viewwe return RemoteViews
	 */
	@Override
	public RemoteViews getViewAt(int position) {

		RemoteViews remoteView = null;
		remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
		if (currentClass == null || currentClass == ListItem.class) {
			Log.e("ListItem", "item" + position);
			ListItem listItem = listItemList.get(position % listItemList.size());
			remoteView.setViewVisibility(R.id.layout_item, View.GONE);
			remoteView.setTextViewText(R.id.heading, /* listItem.heading */"");

			remoteView.setTextViewText(R.id.content, /* listItem.content */"");
			remoteView.setBitmap(R.id.imageView, "setImageBitmap", BitmapFactory.decodeResource(context.getResources(), listItem.imageId));
			return remoteView;
		}
		try {
			if (position > daysInfo.list.length)
				return remoteView;
			if (currentClass == DaysInfo.class) {
				// Log.e("DayInfo", "item"+position);
				DayInfo day = daysInfo.list[position % daysInfo.list.length];
				remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
				Calendar cal = Calendar.getInstance(); 
				//Set time in milliseconds
				cal.setTimeInMillis(day.dt*1000);
				TimeUtil time = new TimeUtil(day.dt*1000);
				String strDate = time.getDayInWeek("vi") + "-" + time.getDayInMonth("vi")/* + "-" + time.getMonthShort("vi")*/;
				
				switch (position) {
				case 0:
					strDate="Hôm nay - "+strDate;
					break;
				default:
					break;
				}
				remoteView.setTextViewText(R.id.heading, strDate);
				String desc = "";
				for (Weather descs : day.weather) {
					desc += Language.getInstance(context).enToVi(descs.description) + " ";
				}

				remoteView.setTextViewText(R.id.content, Temp.getCTemp(day.temp.day) + "" + (char) 0x00B0 + "C - " + desc);
				String icon = day.weather[0].icon;
				int defIcon = 0;
				for (Object[] item : Constant.WEATHER_DRAWABLE) {
					if (item[0].toString().equals(icon)) {
						defIcon = (Integer) item[1];
					}
				}
				remoteView.setBitmap(R.id.imageView, "setImageBitmap", BitmapFactory.decodeResource(context.getResources(), defIcon == 0 ? R.drawable.rainy : defIcon));
			}
		} catch (Exception e) {

		}
		return remoteView;
	}

	@Override
	public RemoteViews getLoadingView() {
		return null;
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public void onCreate() {
	}

	@Override
	public void onDataSetChanged() {
		
	}

	@Override
	public void onDestroy() {
		if (listItemList != null)
			listItemList.clear();
		listItemList = null;
	}

}
